<?php

/**
 * 商品处理
 */

namespace app\shop\common;

class ShopCommon extends \app\member\common\MemberCommon {


    protected function addCart() {
        $data = request('post');
        if(!target('shop/Shop', 'service')->addCart($this->userInfo['user_id'], $data)){
            $this->error(target('shop/Shop', 'service')->getError());
        }
        $this->success('添加商品成功!');
    }

    protected function addFollow() {
        $data = request('post');
        $msg = target('shop/Shop', 'service')->addFollow($this->userInfo['user_id'], $data);
        if(!$msg){
            $this->error(target('shop/Shop', 'service')->getError());
        }
        $this->success($msg);
    }

    protected function addFaq() {
        $id = request('post', 'shop_id', 0, 'intval');
        $info = target('shop/Shop')->getInfo($id);
        if(empty($info) || !$info['status']) {
            $this->error('商品不存在！');
        }
        $content = request('post','content', '', 'html_clear');
        $content = trim($content);
        if(empty($content)) {
            $this->error('您咨询的问题不能为空！');
        }
        if(mb_strlen($content) > 100) {
            $this->error('请保持问题在100个文字内！');
        }

        $prevInfo = target('shop/ShopFaq')->loadList([
            'A.user_id' => $this->userInfo['user_id'],
            'A.has_id' => $id,
            'A.app' => 'shop',
        ],1,'time desc');
        $prevInfo = $prevInfo[0];

        if(!empty($prevInfo)) {
            if(empty($prevInfo['replay_time'])) {
                $this->error('您的上个问题暂无回复，无法咨询新的问题！');
            }
        }

        $status = target('shop/ShopFaq')->add([
            'user_id' => $this->userInfo['user_id'],
            'has_id' => $id,
            'app' => 'shop',
            'time' => time(),
            'content' => $content
        ]);
        if(!$status){
            $this->error('咨询问题失败！');
        }
        $this->success('问题发布成功，请等待回复！');
    }

}